package cn.iocoder.yudao.module.dispatch.service.runningstate;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.dispatch.controller.admin.runningstate.vo.*;
import cn.iocoder.yudao.module.dispatch.dal.dataobject.runningstate.RunningStateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.dispatch.convert.runningstate.RunningStateConvert;
import cn.iocoder.yudao.module.dispatch.dal.mysql.runningstate.RunningStateMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dispatch.enums.ErrorCodeConstants.*;

/**
 * 参数 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class RunningStateServiceImpl implements RunningStateService {

    @Resource
    private RunningStateMapper runningStateMapper;

    @Override
    @DS("mssql")
    public List<RunningStateDO> findAll() {
        return runningStateMapper.selectList();
    }

    @Override
    @DS("mssql")
    public List<RunningStateDO> findByPage(Integer page) {
        return runningStateMapper.selectList("page", page);
    }

    @Override
    @DS("mssql")
    public Integer createRunningState(RunningStateCreateReqVO createReqVO) {
        // 插入
        RunningStateDO runningState = RunningStateConvert.INSTANCE.convert(createReqVO);
        runningStateMapper.insert(runningState);
        // 返回
        return runningState.getId();
    }

    @Override
    @DS("mssql")
    public void updateRunningState(RunningStateUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRunningStateExists(updateReqVO.getId());
        // 更新
        RunningStateDO updateObj = RunningStateConvert.INSTANCE.convert(updateReqVO);
        runningStateMapper.updateById(updateObj);
    }

    @Override
    @DS("mssql")
    public void deleteRunningState(Integer id) {
        // 校验存在
        this.validateRunningStateExists(id);
        // 删除
        runningStateMapper.deleteById(id);
    }

    private void validateRunningStateExists(Integer id) {
        if (runningStateMapper.selectById(id) == null) {
            throw exception(RUNNING_STATE_NOT_EXISTS);
        }
    }

    @Override
    @DS("mssql")
    public RunningStateDO getRunningState(Integer id) {
        return runningStateMapper.selectById(id);
    }

    @Override
    @DS("mssql")
    public List<RunningStateDO> getRunningStateList(Collection<Integer> ids) {
        return runningStateMapper.selectBatchIds(ids);
    }

    @Override
    @DS("mssql")
    public PageResult<RunningStateDO> getRunningStatePage(RunningStatePageReqVO pageReqVO) {
        return runningStateMapper.selectPage(pageReqVO);
    }

    @Override
    @DS("mssql")
    public List<RunningStateDO> getRunningStateList(RunningStateExportReqVO exportReqVO) {
        return runningStateMapper.selectList(exportReqVO);
    }

}
